package cn.lyq.card.ui.agent

import androidx.core.content.ContextCompat
import cn.lyq.card.R
import cn.lyq.card.data.model.AgentBuyInfo
import com.blankj.utilcode.util.SpanUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder


class AgentBuyAdapter : BaseQuickAdapter<AgentBuyInfo, BaseViewHolder>(
    R.layout.item_agent_buy, mutableListOf()
), LoadMoreModule {

    var key = ""

    override fun convert(holder: BaseViewHolder, item: AgentBuyInfo) {
        setStoreKeyName(item.agentName ?: "代理", holder)
        holder.setText(R.id.tvDeviceType, item.goodsName ?: "机具")
        holder.setText(R.id.tvDeviceNum, "${item.goodsNumber}台")
        holder.setText(R.id.tvPaymentDate, item.payTime)
    }

    private fun setStoreKeyName(item: String, holder: BaseViewHolder) {
        if (key.isEmpty() || !item.contains(key)) {
            holder.setText(R.id.tvName, item)
        } else {
            val temp = item.split(key)
            val utils = SpanUtils.with(holder.getView(R.id.tvName))
            temp.withIndex().forEach {
                if (it.value.isNotEmpty()) {
                    utils.append(it.value)
                }
                if (it.index != temp.size - 1) {
                    utils.append(key)
                        .setForegroundColor(ContextCompat.getColor(context, R.color.font_blue))
                }
            }
            utils.create()
        }
    }
}